package com.guigu.basedservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.basedservice.dto.BasePartsTypeDTO2;
import com.guigu.basedservice.dto.BasePartsTypeDto;
import com.guigu.basedservice.dto.TreeNode;
import com.guigu.basedservice.entity.BasePartsType;
import com.guigu.basedservice.mapper.BasePartsTypeMapper;
import com.guigu.basedservice.service.BasePartsTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.basedservice.vo.BasePartsTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 配件目录 服务实现类
 * </p>
 *
 * @author Heyuhang
 * @since 2024-09-13
 */
@Service
public class BasePartsTypeServiceImpl extends ServiceImpl<BasePartsTypeMapper, BasePartsType> implements BasePartsTypeService {

    @Autowired(required = false)
    private BasePartsTypeMapper basePartsTypeMapper;

    @Override
    public PageInfo<BasePartsType> show( BasePartsTypeVo vo, Integer currentPage, Integer pageSize) {
        QueryWrapper<BasePartsType> queryWrapper=new QueryWrapper<>();
        if (vo!=null) {
            if (vo.getName() != null && vo.getName() != "") {
                queryWrapper.eq("name", vo.getName());
            }
            if (vo.getUsableFlag() != null && vo.getUsableFlag() != "") {
                queryWrapper.eq("usable_flag", vo.getUsableFlag());
            }
        }
        PageHelper.startPage(currentPage,pageSize);
        List<BasePartsType> list=basePartsTypeMapper.selectList(queryWrapper);
        PageInfo<BasePartsType> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<BasePartsType> getTreeStructure() {
        // 获取所有配件类型
        List<BasePartsType> allParts = baseMapper.selectList(null);
        // 根节点列表
        List<BasePartsType> rootNodes = allParts.stream()
                .filter(partsType -> partsType.getParentTypeUuid() == null || partsType.getParentTypeUuid().isEmpty())
                .collect(Collectors.toList());
        // 递归构建树
        rootNodes.forEach(root -> buildTree(root, allParts));
        return rootNodes;
    }

    @Override
    public List<BasePartsType> findByCondition(String name) {
        QueryWrapper<BasePartsType> wrapper = new QueryWrapper();
        wrapper.eq("name",name);
        List<BasePartsType> basePartsTypes = basePartsTypeMapper.selectList(wrapper);
        return basePartsTypes;
    }

    // 递归方法
    private void buildTree(BasePartsType parentNode, List<BasePartsType> allNodes) {
        List<BasePartsType> children = allNodes.stream()
                .filter(partsType -> parentNode.getPartTypeUuid().equals(partsType.getParentTypeUuid()))
                .collect(Collectors.toList());
        if (!children.isEmpty()) {
            parentNode.setChildren(children);
            children.forEach(child -> buildTree(child, allNodes));
        }
    }


    @Override
    public int addManyBasePartsType(List<BasePartsType> list) {
        return 0;
    }

    // 获取根节点
    @Override
    public List<BasePartsType> getRootNodes() {
        return basePartsTypeMapper.getRootNodes();
    }

    // 获取子节点
    @Override
    public List<BasePartsType> getChildNodes(String parentId) {
        return basePartsTypeMapper.getChildNodes(parentId);
    }

    @Override
    public List<BasePartsType> getRecursiveSql() {
        return basePartsTypeMapper.getRecursiveSql();
    }


}
